package com.wwwarehouse.usercenter.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.wwwarehouse.common.activity.base.BaseActivity;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.constant.Constant;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.usercenter.R;
import com.wwwarehouse.usercenter.constant.UserCenterConstant;

import java.util.HashMap;
import java.util.Map;

import static com.wwwarehouse.usercenter.constant.UserCenterConstant.URL_CONFIRM_LOGIN;

/**
 * Created by Jackie on 2017/9/7.
 * PC 确认登录
 */

@Route(path = UserCenterConstant.PATH_PC_LOGIN_CONFIRM_ACTIVITY)
public class PCLoginConfirmActivity extends BaseActivity implements View.OnClickListener {
    private ImageView mCloseImg;
    private Button mConfirmBtn;

    private String mSecurityQrCode;

    private static final int REQUEST_CONFIRM_LOGIN = 0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_pc_login_confirm);

        initView();
        initEvent();
    }

    private void initView() {
        mCloseImg = findView(R.id.iv_close);
        mConfirmBtn = findView(R.id.btn_confirm);

        mSecurityQrCode = getIntent().getStringExtra(Constant.KEY_SECURITY_QR_CODE);
    }

    private void initEvent() {
        mCloseImg.setOnClickListener(this);
        mConfirmBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();

        if (id == R.id.iv_close) {
            finishActivity();
        } else if (id == R.id.btn_confirm) {
            //确认登录
            confirmLogin();
        }
    }

    private void confirmLogin() {
        Map map = new HashMap();
        map.put("securityQrCode", mSecurityQrCode);

        // 扫码确认登录
        NoHttpUtils.httpPost(URL_CONFIRM_LOGIN, map, mOnResponseListener, REQUEST_CONFIRM_LOGIN);
    }

    NoHttpUtils.OnResponseListener mOnResponseListener = new NoHttpUtils.OnResponseListener() {
        @Override
        public void onSucceed(CommonClass commonClass, int what) {
            switch (what) {
                case REQUEST_CONFIRM_LOGIN:
                    if ("0".equals(commonClass.getCode())) {
                        //登录成功
                        finishActivity();
                    } else {
                        toast(commonClass.getMsg());
                    }
                    break;
                default:
                    break;
            }
        }

        @Override
        public void onFailed(String result, int what) {
            toast(result);
        }

        @Override
        public void onFinish(int what) {

        }
    };
}
